﻿var loc;
var sn0;
var sn;
var nam = document.title;
function OnComplete(sc, searcher) {
    if (searcher.results && searcher.results.length > 0) {
        var gc = document.getElementById("googlecontrol");
        if (gc) gc.style.display = "block";
    }
}
function GetXhr() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    }
    return new ActiveXObject("Microsoft.XMLHTTP");
}

function vote(zhichi) {
    var xmlHttp = GetXhr();

    var url = "Vote.ashx?zhichi=" + ((zhichi) ? "1" : "0");
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            if (xmlHttp.status == 200) {
            }
        }
    }
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null)
}

function OnLoad() {
    sn = encodeURIComponent(sn0);
    if (google) {
        var sc = new google.search.SearchControl();
        var options = new google.search.SearcherOptions();
        options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
        var searcher = new google.search.WebSearch();
        searcher.setRestriction(google.search.Search.RESTRICT_SAFESEARCH, google.search.Search.SAFESEARCH_OFF);
        searcher.setRestriction(google.search.Search.RESTRICT_EXTENDED_ARGS, { "filter": "0" });
        sc.addSearcher(searcher, options);
        sc.draw(document.getElementById("searchcontrol"));
        sc.setSearchCompleteCallback(this, OnComplete);
        sc.execute(sn0);

        geocoder = new google.maps.Geocoder();
        if (geocoder) {
            var cityname = document.getElementById("cityname");
            if (!cityname || !cityname.firstChild) return;
            var city = cityname.firstChild.nodeValue;
            geocoder.geocode({ 'address': city, 'language': "zh-CN", 'country': "cn" }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK && results.length) {
                    if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
                        var mc = document.getElementById("map_canvas");
                        mc.style.display = "block";
                        var latlng = results[0].geometry.location;
                        var myOptions = { zoom: 7, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP }
                        map = new google.maps.Map(mc, myOptions);
                        var marker = new google.maps.Marker({
                            position: latlng,
                            map: map
                        });
                    }
                }
            });
        }
    }

    requestScript = document.createElement("script");
    requestScript.type = "text/javascript";
    requestScript.src = "http://api.search.live.net/json.aspx?AppId=AF68902E9CADADB92A5805EDDB7EBEE8176A53F7&Query=" + sn
    + "&Sources=Web&Market=zh-CN&Adult=Off&Options=EnableHighlighting&Web.Count=4&Web.Options=DisableHostCollapsing&JsonType=callback&JsonCallback=LiveSearchCompleted";
    document.getElementsByTagName("head")[0].appendChild(requestScript);

    var requestScript = document.createElement("script");
    requestScript.type = "text/javascript";
    requestScript.src = 'http://boss.yahooapis.com/ysearch/web/v1/' + sn + '?format=json&count=4&callback=YahooSearchCompleted&region=cn&lang=szh&' +
           'appid=v4CQtifV34F1Vitkv03Y2vQVAqiESDOl5PG_IQu.NwmMCKLKUHYxxx7JBNmJyqZooQgoou0-';
    document.getElementsByTagName("head")[0].appendChild(requestScript);
}
function YahooSearchCompleted(response) {
    var results = response.ysearchresponse.resultset_web;
    var output = document.getElementById("yahoocontrol");
    var resultsList = document.createElement("div");
    resultsList.className = "gsc-control";
    output.appendChild(resultsList);
    if (results) {
        output.style.display = "block";
        for (var i = 0; i < results.length; ++i) {
            var listItem = document.createElement("div");
            listItem.className = "gsc-result";
            listItem.innerHTML = genResultStr(results[i].clickurl, results[i].title, results[i].abstract, results[i].dispurl);
            resultsList.appendChild(listItem);
        }
        if (results.length == 4) {
            var moreLink = document.createElement("div");
            moreLink.innerHTML = "<a target=\"_blank\" href=\"http://search.yahoo.com/search?p=" + sn + "\">更多结果&nbsp;»</a>";
            resultsList.appendChild(moreLink);
        }
    }
}
function LiveSearchCompleted(response) {
    if (!response.SearchResponse.Errors) {
        var output = document.getElementById("bingcontrol");
        var resultsList = document.createElement("div");
        resultsList.className = "gsc-control";
        output.appendChild(resultsList);
        var results = response.SearchResponse.Web.Results;
        if (results) {
            output.style.display = "block";
            for (var i = 0; i < results.length; ++i) {
                var resultsListItem = document.createElement("div");
                resultsListItem.className = "gsc-result";
                resultsList.appendChild(resultsListItem);
                var resultStr = genResultStr(results[i].Url, results[i].Title, results[i].Description, results[i].Url);
                resultsListItem.innerHTML = resultStr.replace(new RegExp("\uE000", "g"), "<b>")
                    .replace(new RegExp("\uE001", "g"), "</b>");
            }
            if (results.length == 4) {
                var moreLink = document.createElement("div");
                moreLink.innerHTML = "<a target=\"_blank\" href=\"http://www.bing.com/search?q=" + sn + "\">更多结果&nbsp;»</a>";
                resultsList.appendChild(moreLink);
            }
        }
    }
}
function genResultStr(clickurl, title, snippet, displayurl) {
    return "<div class=\"gs-title\"><a class=\"gs-title\" href=\""
                + clickurl + "\" target=\"_blank\">"
                + title + "</a></div><div>"
                + snippet + "</div><div class=\"gs-visibleUrl\">"
	    		+ displayurl + "</div>";
}




var userip;
var pat;
var detail;
var reporter;
var query;
var m = location.href.match(/\?number=(.*)/);
if (m && m[1].length >= 8) {
    query = m[1];
}
function presubmit() {
    if (document.getElementById("form1").number.value.length < 8) {
        alert("请正确输入您要报告的电话号码");
        return false;
    }

    var radio = document.getElementById("form1").type;
    var k = radio.length;

    if (!(detail.value)) {
        if (radio[k - 1].checked) {
            alert("请输入您的评论！");
            detail.focus();
            return false;
        } else if (!confirm("您没有填写任何内容，此条举报将无法出现在首页！仍然发布请点确定，返回填写请点取消。")) {
            detail.focus();
            return false;
        }
    }

    focusReporter();
    var otherValue = document.getElementById("other").value;
    if (radio[k - 2].checked) {
        if (otherValue) radio[k - 2].value = otherValue;
    }
    return true;
}
function focusReporter() {
    if (reporter.value == userip) {
        reporter.value = "";
        reporter.style.color = "#000000";
    }
}
function blurReporter() {
    if (reporter.value == "") {
        reporter.value = userip;
        reporter.style.color = "#808080";
    }
}
function inputDetail() {
    detail.disabled = false;
    detail.style.display = "block";
    document.getElementById("notice").style.display = "none";
    detail.focus();
}
function hideDetail() {
    detail.disabled = true;
    detail.style.display = "none";
    document.getElementById("notice").style.display = "block";
}
function clickOther() {
    inputDetail();
    var other = document.getElementById("other");
    other.style.display = "inline";
    other.focus();
}
function expand() {
    detail.rows = 5;
}
function getMore(str) {
    var result = [];
    var m = str.match(pat);
    if (!m) return result;
    for (var i = 0; i < m.length; i++) {
        if (m[i] != query && m[i].length < 20 && (i == 0 || m[i] != m[0])) {
            result.push(m[i]);
            if (result.length >= 2) break;
        }
    }
    return result;
}
function detailChange() {
    var nums = getMore(detail.value);
    var morediv = document.getElementById("morediv");
    morediv.innerHTML = "";
    var inputMore = "";
    for (var i = 0; i < nums.length; i++) {
        var itemdiv = document.createElement("div");
        var ch = document.createElement("input");
        ch.type = "checkbox";
        ch.defaultChecked = true;
        ch.value = nums[i];
        itemdiv.appendChild(ch);
        itemdiv.appendChild(document.createTextNode(nums[i]));
        morediv.appendChild(itemdiv);
        inputMore += nums[i] + ",";
    }
    morediv.parentNode.style.display = (nums.length > 0) ? "block" : "none";
    document.getElementById("more").value = inputMore;
}
